package com.bgate.utils;


import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Binh
 */
public class BgateCanvas extends Canvas implements MessageListener {

    private Timer timer1;
    private TimerTask timerTask;
    public boolean isMenu = true;
    int curMenu = 0, langId = 1, minMenu = 0;
    int xL = 0, yL = 0;
    public String[] menu, menuViet = new String[]{"BẮT ĐẦU", "TẮT ÂM THANH", "ENGLISH", "THOÁT"};
    public String[] menuEnglish = new String[]{"START GAME", "SOUND ON", "TIẾNG VIỆT", "EXIT"};
    Font fontMenu = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
    Font fontTextMenu = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
    String msgReceived = "no Message";
    MessageConnection serverConn;
    
//    PiPoOnline midlet;
    boolean sound;
    int gold = 1000;
    private int style = 1;
    byte[] text;
    public static int DELAY_DEFAULT = 100;
    private int color;
    
//    private int color = CustomFont.COLOR_ORIGINAL;
//    private byte size = CustomFont.SIZE_SMALL;
    
    private int mode = 0;
//    private byte margin = CustomFont.MARGIN_LEFT;
    Image bkImage = null;

    public BgateCanvas() {
    }

    public BgateCanvas(/*PiPoOnline midlet, */boolean sound) {
        this.setFullScreenMode(true);
//        this.midlet = midlet;
        this.sound = sound;


        timer1 = new Timer();

        timerTask = new TimerTask() {

            public void run() {
                if (isShown()) {
                    if (true) {
                        if (xL < 10) {
                            xL++;
                        } else {
                            xL = 0;
                        }

                        if (yL < 10) {
                            yL++;
                        } else {
                            yL = 0;
                        }

                        repaint(0, 0, getWidth(), getHeight());
                    }
                }
            }
        };
        timer1.schedule(timerTask, 0, 100);
    }

    protected void paint(Graphics g) {
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, getWidth(), getHeight());
        //Draw background        
//         g.drawRegion(bkImage, (240-getWidth())/2, (320-getHeight())/2, getWidth(), getHeight(), 0, 0, 0, 0);
        int width = getWidth();
        int height = getHeight();
        if (width > 240) {
            width = 240;
        }
        if (height > 320) {
            height = 320;
        }
        if (isMenu) {
            if (this.getWidth() > 160) {
                if (bkImage == null) {
                    try {
                        bkImage = Image.createImage("/menu.png");
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                g.drawRegion(bkImage, (240 - width) / 2, (320 - height) / 2, width, height, 0, Math.abs(getWidth() - Math.min(getWidth(), 240)) / 2, Math.abs(getHeight() - Math.min(getHeight(), 320)) / 2, 0);
                drawMenu(g, getWidth() / 2, (getHeight() - fontMenu.getHeight() * 3) / 2);
            } else {
                drawMenuSmall(g, getWidth() / 2, (getHeight() - fontMenu.getHeight() * 4) / 2);
            }

        } else {
            g.setColor(255, 255, 255);
            g.drawString(msgReceived, getWidth() / 29, getHeight() / 2, 0);
        }
    }
    public static int VERTICAL = 1;
    public static int HORIZONTAL = 0;

    public void drawMenuSmall(Graphics g, int x, int y) {

        VERTICAL = 1;
        HORIZONTAL = 0;
        int color1 = makeColorAsInt(0, 255, 0, 0);
        // int color1 = makeColorAsInt(0, 0, , 0);
        int color2 = makeColorAsInt(0, 255, 255, 255);
        int colorL = makeColorAsInt(0, 255, 255, 0);

        g.setColor(0xffffff);
        g.setColor(255, 225, 0);
        for (int q = 1; q <= getWidth() / 4 + 1; q++) {
            for (int p = 1; p <= 2; p++) {
                if ((p == 1) || ((q % 2) == 0)) {
                    g.fillRect(q * 3 - 1 + getWidth() / 8, p * 3 + (getHeight() - 7 * fontMenu.getHeight()) / 2 - 4, 2, 2);

                }
                if ((p == 2) || ((q % 2) == 0)) {
                    g.fillRect(q * 3 - 1 + getWidth() / 8, p * 3 + (getHeight() + 7 * fontMenu.getHeight()) / 2 - 4, 2, 2);
                }
            }
        }

        for (int q = 1; q <= 2; q++) {
            for (int p = 1; p <= 7 * fontMenu.getHeight() / 3 + 1; p++) {
                if ((q == 1) || (p % 2) == 0) {
                    g.fillRect(q * 3 - 1 + getWidth() / 8, p * 3 + (getHeight() - 7 * fontMenu.getHeight()) / 2 - 4, 2, 2);
                }
                if ((q == 2) || ((p % 2) == 0)) {
                    g.fillRect(q * 3 - 1 + getWidth() / 4 * 3 + getWidth() / 8 - 6 + 3, p * 3 + (getHeight() - 7 * fontMenu.getHeight()) / 2 - 4, 2, 2);
                }
            }
        }
        if (langId == 0) {
            menu = menuEnglish;
        } else {
            menu = menuViet;
        }

        g.setFont(fontTextMenu);

        for (int i = minMenu; i < minMenu + 4; i++) {
            if (curMenu == i) {
                g.setColor(0xffffff);
                color = 0;
            } else {
                g.setColor(0xff0000);
                color = 0xffffffff;
            }
            if (getWidth() < 160) {
//                CustomFont.drawString(g, menu[i], 0, 400, color, CustomFont.SIZE_SMALL, CustomFont.STYLE_BOLD, CustomFont.MARGIN_CENTER, 0, y - 6 + (i - minMenu) * 16, getWidth(), getHeight());
                g.drawString(menu[i], x - fontTextMenu.stringWidth(menu[i]) / 2, y + (i - minMenu) * (fontMenu.getHeight() * 57 / 56), 0);
            }
//            g.drawString(menu[i], x - fontTextMenu.stringWidth(menu[i]) / 2, y + (i - minMenu) * (fontMenu.getHeight() * 57 / 56), 0);
        }
    }

    public void drawMenu(Graphics g, int x, int y) {

        if (langId == 0) {
            menu = menuEnglish;
        } else {
            menu = menuViet;
        }

//        if (midlet.sound) {
//            if (langId == 1) {
//                menu[1] = "TẮT ÂM THANH";
//            } else {
//                menu[1] = "SOUND OFF";
//            }
//        } else {
//            if (langId == 1) {
//                menu[1] = "BẬT ÂM THANH";
//            } else {
//                menu[1] = "SOUND ON";
//            }
//        }

        g.setFont(fontTextMenu);
        for (int i = minMenu; i < minMenu + 4; i++) {
            if (curMenu == i) {
                color = 10;
            } else {
                color = 1;
            }
            text = new byte[menu[i].length()];
//            Designer.toBytesIndex(menu[i], text);
//            Designer.drawString(g, text, 0, menu[i].length(), Designer.FONT_TEXT, color, getWidth() / 2 - (menu[i].length() - 1) * 4, y - 6 + (i - minMenu) * 16);
//            Designer.drawCenterString(g, text, 0, menu[i].length(), color, this.getWidth() / 2, y - 6 + (i - minMenu) * 20);
        }
        Runtime.getRuntime().gc();
    }

    public static void gradientBox(Graphics g, int color1, int color2, int left, int top, int width, int height, int orientation) {
        int max = orientation == VERTICAL ? height : width;

        for (int i = 0; i < max; i++) {
            int color = midColor(color1, color2, max * (max - 1 - i) / (max - 1), max);

            g.setColor(color);

            if (orientation == VERTICAL) {
                g.drawLine(left, top + i, left + width - 1, top + i);
            } else {
                g.drawLine(left + i, top, left + i, top + height - 1);
            }
        }
    }

    static int midColor(int color1, int color2, int prop, int max) {
        int red =
                (((color1 >> 16) & 0xff) * prop
                + ((color2 >> 16) & 0xff) * (max - prop)) / max;

        int green =
                (((color1 >> 8) & 0xff) * prop
                + ((color2 >> 8) & 0xff) * (max - prop)) / max;

        int blue =
                (((color1 >> 0) & 0xff) * prop
                + ((color2 >> 0) & 0xff) * (max - prop)) / max;

        int color = red << 16 | green << 8 | blue;

        return color;
    }

    private int makeColorAsInt(int alpha, int red, int green, int blue) {
        return alpha << 24 | red << 16 | green << 8 | blue;
    }

    protected void keyPressed(int keyCode) {

        int instantkeyCode = KeyCodeAdapter.getInstance().adoptKeyCode(keyCode);

        switch (instantkeyCode) {
            case KeyCodeAdapter.LEFT_KEY:
                //leftkey();

                repaint();
                return;
            case KeyCodeAdapter.RIGHT_KEY:
                // rightkey();
                repaint();

                return;
            case KeyCodeAdapter.DOWN_KEY:
                downKey();
                repaint();

                return;

            case KeyCodeAdapter.UP_KEY:
                upKey();
                repaint();

                return;
            case KeyCodeAdapter.CENTER_KEY:
                fireKey();
                repaint();

                return;

            case KeyCodeAdapter.SOFT_KEY_MIDDLE_INTERNET:
                fireKey();
                repaint();

                return;

            case KeyCodeAdapter.SOFT_KEY_LEFT:
                menuLeftKey();
                repaint();

                return;

            case KeyCodeAdapter.SOFT_KEY_RIGHT:
                menuRightKey();
                repaint();

                return;
            case KeyCodeAdapter.KEY_2:
                upKey();
                repaint();
                return;

            case KeyCodeAdapter.KEY_8:
                downKey();
                repaint();
                return;
            case KeyCodeAdapter.KEY_5:
                fireKey();
                repaint();
                return;

        }
    }

    public void upKey() {

        if (curMenu > 0) {
            if (minMenu == curMenu) {
                minMenu--;
            }
            curMenu--;
        } else {
            curMenu = menu.length - 1;
            minMenu = menu.length - 4;
        }

    }

    public void downKey() {

        if (curMenu < (menu.length - 1)) {

            if (curMenu == minMenu + 4) {
                minMenu++;
            }
            curMenu++;

        } else {
            curMenu = 0;
            minMenu = 0;
        }
    }

    public void fireKey() {
        if (isMenu) {
            switch (curMenu) {
                case 0:
                    try {
                        
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    return;
                case 1:
                    try {
//                        midlet.sound = !midlet.sound;
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    return;
                case 2:
                    if (this.langId == 0) {
                        this.langId = 1;
                    } else {
                        this.langId = 0;
                    }
                    return;
                case 3:
                    return;
                case 4:
                    return;
                case 6:
                    return;
                case 5:
                    return;
                case 7:
                    return;
            }
        }
    }

    public void menuLeftKey() {
//        isMenu = !isMenu;
    }

    public void menuRightKey() {
    }

    public void notifyIncomingMessage(MessageConnection conn) {
        Message msg = null;
        //  Try reading (maybe block for) a message
        try {
            msg = conn.receive();
        } catch (Exception e) {
            // Handle reading errors
            msgReceived = e.toString();
            System.out.println("processMessage.receive " + e);
        }
        // Process the received message
        if (msg instanceof TextMessage) {
            TextMessage tmsg = (TextMessage) msg;
            msgReceived = tmsg.getPayloadText();
        } else {
            if (msg instanceof BinaryMessage) {
                BinaryMessage bmsg = (BinaryMessage) msg;
                byte[] data = bmsg.getPayloadData();
                //  Handle the binary message...
                msgReceived = data.toString();

            }
        }
    }
}
